package cn.yo2.irain.log4money.app.accountbooks;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AccountDAOImpl extends HibernateDaoSupport implements IAccountDAO{
	
	private static final Log log = LogFactory.getLog(AccountDAOImpl.class);

	public void deleteItem(Account account) throws RuntimeException {
		log.debug("deleting Account instance");
		getHibernateTemplate().delete(account);
		log.debug("delete successful");
	}

	public void saveAccount(Account account) throws RuntimeException {
		log.debug("saving Account instance");
		getHibernateTemplate().save(account);
		log.debug("save successful");
	}

    public void deleteAccount(Account account) throws RuntimeException {
        log.debug("delete Account instance");
		getHibernateTemplate().delete(account);
		log.debug("delete successful");
    }

    public void updateAccount(Account account) throws RuntimeException {
		log.debug("update account ");
		this.getHibernateTemplate().update(account);
		log.debug("update successful");
	}

	public Account findById(String id){
		log.debug("getting Account instance with id: " + id);
        try {
            Account instance = (Account) this.getHibernateTemplate()
                    .get("cn.yo2.irain.log4money.app.accountbooks.Account", id);
            
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
	}

	public List findAll(String ownUser) throws RuntimeException {
		
		String hql = "from Account as a where a.ownUser = '" + ownUser + "'";
		List list = getHibernateTemplate().find(hql);
		
		return list;
	}
	
	public List findInAccount(){
		return findByProperty("inOut", "0");
	}
	
	public List findOutAccount(){
		return findByProperty("inOut", "1");
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Account instance with property: " + propertyName
				+ ", value: " + value);
        String queryString = "from Account i where i."+ propertyName + "='" + value + "'";

        
        return getHibernateTemplate().find(queryString);
	}
	
	public Account findByAccountName(String name) {
		return (Account)findByProperty("accountName", name).get(0);
	}

}
